package Texas;

import java.util.*;
import Texas.Card.*;


public class HandChecker {
	
	public float hasRoyalFlush(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue()+1 == cards.get(i-1).getRank().getValue() && 
					cards.get(i).getSuit().getValue() == cards.get(i-1).getSuit().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				count++;
			}
		}
		
		if(count == 5)
			return 8+(hiCard/100);
		return 0;
	}
	
	public float hasFourOfAKind(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue() == cards.get(i-1).getRank().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				
				if(count < 4) {
					count++;
				}
			} else if(count < 4) {
				count = 1;
			}
		}
		
		if(count == 4)
			return 7+(hiCard/100);
		return 0;
	}
	
	public float hasFullHouse(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue() == cards.get(i-1).getRank().getValue()) {
				if(count == 3) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				if((count == 2 && (cards.get(i).getRank().getValue() == cards.get(i-2).getRank().getValue() ||
						cards.get(i).getRank().getValue() == cards.get(i+1).getRank().getValue())) || count == 1) {
					count++;
				}
			}
		}
		
		if(count == 3)
			return 6+(hiCard/100);
		return 0;
	}
	
	public float hasFlush(ArrayList<Card> cards) {
		int count;
		float hiCard = 0;
		
		for (CardSuit cardSuit : CardSuit.values()) {
			count = 0;
			for(int i = 0; i < cards.size(); i++) {
				if(cards.get(i).getSuit().equals(cardSuit)) {
					if(count == 0) {
						hiCard = cards.get(i).getRank().getValue();
					}
					count++;
				}
			}
			
			if(count == 5)
				return 5+(hiCard/100);
		}
		return 0;
	}
	
	public float hasStreet(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue()+1 == cards.get(i-1).getRank().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				count++;
			} else if(count < 5 && cards.get(i).getRank().getValue() != cards.get(i-1).getRank().getValue()) {
				count = 1;
			}
		}
		
		if(count == 5)
			return 4+(hiCard/100);
		return 0;		
	}
	
	public float hasThreeOfAKind(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue() == cards.get(i-1).getRank().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				count++;
			} else if(count < 3) {
				count = 1;
			}
		}
		
		if(count == 3)
			return 3+(hiCard/100);
		return 0;				
	}
	
	public float hasTwoPair(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;

		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue() == cards.get(i-1).getRank().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				count++;
			}
		}
		
		if(count == 3)
			return 2+(hiCard/100);
		return 0;				
	}
	
	public float hasOnePair(ArrayList<Card> cards) {
		int count = 1;
		float hiCard = 0;
		
		for(int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getRank().getValue() == cards.get(i-1).getRank().getValue()) {
				if(count == 1) {
					hiCard = cards.get(i-1).getRank().getValue();
				}
				count++;
			}
		}
		
		if(count == 2) 
			return 1+(hiCard/100);
		return 0;						
	}
}
